package com.ljm.boot.helloword.letCode;

import java.util.HashMap;

public class Test560_no {

    public static int subarraySum(int[] nums, int k) {
        int count = 0;
        for (int i = 0; i < nums.length; ++i) {
            for (int j = i+1;j< nums.length; ++j) {
                if (nums[i]+nums[j] == k) {
                    System.out.println(i+"-"+j);
                    count++;
                    break;
                }
            }
        }
        return count;
    }
    public static int subarraySum2(int[] nums, int k) {
        int count = 0, pre = 0;
        HashMap< Integer, Integer > mp = new HashMap < > ();
        mp.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            if (mp.containsKey(pre - k)) {
                count += mp.get(pre - k);
            }
            mp.put(pre, mp.getOrDefault(pre, 0) + 1);
        }
        return count;

    }


    public static void main(String[] args) {
        int[] nums = new int[]{1,1,1};
        int k = 2;
        System.out.println(subarraySum(nums,k));
    }
}
